<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
use app\common\model\User;
use think\facade\View;

class Index extends BaseController
{
    public function index1()
    {
        //add
        /**
        $res=Db::name('user')->insert([
            'name'=>'beijing'.rand(),
            'number'=>rand()
        ]);
         */

        //update
        /** 
        $res=Db::name('user')->where('id',1)->update([
            'name'=>'nanjing',
            'number'=>1
        ]);
        */
        //delete
        //$res=Db::name('user')->where('id',1)->delete();

        //find
        //$res=Db::name('user')->select()->toArray();
        //$res=Db::name('user')->find(2);

        //model add
        /** 
        $user = new User();
        $user->name = 'shanghai';
        $user->number = 3;
        $res = $user->save();    
            */
        //model update
        /** 
        $user =User::find(3);
        $user->name = 'guangzhou';
        $user->number = 5;
        $res = $user->save(); 
        */
        // model delete
        /**
        $user =User::find(3);
        $res = $user->delete();
         */    
        //model find
        $res=User::find(4)->toArray();    
        $res=User::select([5,6])->toArray();

        halt($res);
        
    }

    public function index(){
        $data = Db::name('user')->select()->toArray();
        //halt($data);

        $number = 8;
        //类加载
        /** 
        return View::fetch('index',[
            'number'=>$number
        ]);
        */
        //助手涵数
        return view('',[
            'data'=>$data
        ]);


    }

    public function form()
    {
        //助手涵数
        return view();
    }
    //ajax提交过来的数据
    public function user()
    {
        $data=input("post.");
        $res=Db::name('user')->insert($data);
        if($res){
            return json(['msg'=>'success','code'=>1]);
        }else{
            return json(['msg'=>'error','code'=>0]);
        }
    }
}
